import {getLyric} from 'api/song'
export default class Song {
  // 歌曲ID，歌手信息，专辑信息，歌曲时长，歌曲图片，播放地址
  constructor({id, singer, name, album, duration, image, url}) {
    this.id = id
    this.singer = singer
    this.name = name
    this.album = album
    this.duration = Math.floor(duration / 1000)
    this.image = image
    this.url = url
  }
  // 根据ID获取歌词
  getLyric() {
    if (this.lyric) {
      return Promise.resolve(this.lyric)
    }
    return new Promise((resolve) => {
      getLyric(this.id).then((res) => {
        this.lyric = res
        resolve(this.lyric)
      })
    })
  }
}

export function createSong(list) {
  return new Song({
    id: list.id,
    singer: filterSinger(list.ar),
    name: list.name,
    album: list.al.name,
    duration: list.dt,
    image: `https://v1.itooi.cn/netease/pic?id=${list.id}`,
    url: `https://v1.itooi.cn/netease/url?id=${list.id}`
  })
}
export function createListSong(musicData) {
  return new Song({
    id: musicData.id,
    singer: filterSinger(musicData.artists),
    name: musicData.name,
    duration: musicData.duration,
    album: musicData.album.name,
    image: `https://v1.itooi.cn/netease/pic?id=${musicData.id}`,
    url: `https://v1.itooi.cn/netease/url?id=${musicData.id}`
  })
}
function filterSinger(singer) {
  let ret = []
  if (!singer) {
    return ''
  }
  singer.forEach((s) => {
    ret.push(s.name)
  })
  return ret.join('/')
}